package com.hbt.orderfood.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hbt.orderfood.R;
import com.hbt.orderfood.db.DBAdapter;
import com.hbt.orderfood.entitys.BigOrder;
import com.hbt.orderfood.entitys.Order;
import com.hbt.orderfood.utils.StringUtils;

import java.util.List;

public class MngOrderAdapter extends RecyclerView.Adapter {
    public List<BigOrder> getBigOrders() {
        return bigOrders;
    }

    List<BigOrder> bigOrders;
    public MngOrderAdapter(List<BigOrder> bigOrders){
        this.bigOrders = bigOrders;
    }
    private Context context;
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        LinearLayout layout = new LinearLayout(parent.getContext());
        layout.setLayoutParams(params);
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.mng_order_item,layout,true);
        ViewHolder holder = new ViewHolder(view);
        //        System.out.println("onCreateViewHolder");
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder hold = (ViewHolder)holder;
        BigOrder bigOrder = bigOrders.get(position);
        hold.tv_username.setText("来自用户："+bigOrder.getUser().getUsername());
        hold.tv_main.setText("      "+ StringUtils.MAIN_STR+"："+ordersConvertToString(bigOrder.getMain_order()));
        hold.tv_veg.setText("      "+StringUtils.VEG_STR+"："+ordersConvertToString(bigOrder.getVeg_order()));
        hold.tv_drink.setText("      "+StringUtils.DRINK_STR+"："+ordersConvertToString(bigOrder.getDrink_order()));
        hold.yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.click(position, bigOrder);
            }
        });
    }

    public interface MngOrderItemClickListener{
        void click(int position, BigOrder bigOrder);
    }

    public void setListener(MngOrderItemClickListener listener) {
        this.listener = listener;
    }

    private MngOrderItemClickListener listener;

    String ordersConvertToString(List<Order> orders){
        String s = "";
        for (Order o:orders){
            s+= ",   "+o.getFood().getName()+" X "+String.valueOf(o.getNum());
        }
        if (s.length()>0){
            s = s.substring(1);
        }

        return s;
    }

    @Override
    public int getItemCount() {
        return bigOrders.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_username;
        TextView tv_main;
        TextView tv_veg;
        TextView tv_drink;
        LinearLayout yes;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_username = itemView.findViewById(R.id.username);
            tv_main = itemView.findViewById(R.id.main);
            tv_veg = itemView.findViewById(R.id.veg);
            tv_drink = itemView.findViewById(R.id.drink);
            yes = itemView.findViewById(R.id.yes);
        }
    }
}
